package vn.hocweb.dao.dao_implement;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vn.hocweb.dao.dao_interface.IBaiHoc_CongCuDAO;

public class BaiHoc_CongCuDAO extends GenericDAO implements IBaiHoc_CongCuDAO{

	@Override
	public List<Long> findAll_danhSachCongCu(long maBH, int firstItem, int batchSize) {
		Connection connection = getConnection();
		List<Long> result = new ArrayList<Long>();
		String sql = "select * from baihoc_congcu where mabaihoc = ? limit ?, ?";
		PreparedStatement pre;
		try {
			pre = connection.prepareStatement(sql);
			pre.setLong(1, maBH);
			pre.setInt(2, firstItem);
			pre.setInt(3, batchSize);
			ResultSet effected = pre.executeQuery();
			while (effected.next()){
				long maCongCu = effected.getLong("macongcu");
				result.add(maCongCu);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean save(long maBaiHoc, long maCongCu) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "insert into baihoc_congcu (macongcu, mabaihoc, hienthi) values (?, ?, ?)";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, maCongCu);
			pre.setLong(2, maBaiHoc);
			pre.setBoolean(3, true);
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean delete(long maBH_CC) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "delete from baihoc_congcu where mabh_cc = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setLong(1, maBH_CC);
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean remove(long maBH_CC) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update baihoc_congcu set hienthi = ? where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, false);
			pre.setLong(2, maBH_CC);
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

	@Override
	public boolean restore(long maBH_CC) {
		Connection connection = getConnection();
		boolean result = false;
		String sql = "update baihoc_congcu set hienthi = ? where macongcu = ?";
		try {
			PreparedStatement pre = connection.prepareStatement(sql);
			pre.setBoolean(1, true);
			pre.setLong(2, maBH_CC);
			
			int effected = pre.executeUpdate();
			if(effected > 0) result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		freeConnection(connection);
		return result;
	}

}
